<?php

require_once 'Zend/Db/Table/Row.php';

class Naneau_Db_Table_Row extends Zend_Db_Table_Row_Abstract {

    /**
     * overridden __get
     *
     * @param string $what
     * @return mixed
     * @throws Zend_Db_Table_Row_Exception
     */
    public function __get($what)
    {
        try {
            $return  = parent::__get($what);
            //it exists as a 'normal' field
        } catch (Zend_Db_Table_Row_Exception $e) {
            //field doesn't exist as 'normal' field

            $return = $this->_getTable()->getExtraField($this, $what);
            //try to get it from the extra fields in the table definition
        }

        return $this->_getTable()->getModifiedValue($this, $what, $return);
        //return modified value (if it can be modified, that is)
    }

    public function __set($what, $value) {
        $value = $this->_getTable()->getUnModifiedValue($this, $what, $value);

        parent::__set($what, $value);
    }

    public function toStringArray() {
        $array = parent::toArray();
        $keys = array_keys($array);

        for ($x = 0, $length = count($array); $x < $length; $x++) {
            $array[$keys[$x]] = (string) $this->_getTable()->getModifiedValue($this, $keys[$x], $array[$keys[$x]]);
        }

        return $array;
    }

    /**
     * try to set values for this row from a mixed array
     * 
     * (one that may contain values that aren't in the row)
     *
     * @param array $values
     * @return void
     */
    public function setFromMixedArray($values) {
        $info = $this->_table->info();
        foreach ($info['cols'] as $col) {
            if (isset($values[$col]) && $values[$col]) {
                $this->$col = $values[$col];
            }
        }
    }

}